local Tick = CurTime()
function GM:Tick( )
	for num,ply in pairs (player.GetAll( )) do
		local plyuni = ply:UniqueID()
		if servertable["ply"][plyuni]["Inpod"] then
			local aimvector = ply:GetCursorAimVector( )
			local aimangle = aimvector:Angle( )
			for k,prop in pairs(servertable["ship"]["controller"]) do 
				if prop:IsValid() then
					if (GetOwnStatus(plyuni,prop) == 1) then 
						if (prop:GetPhysicsObject():IsMoveable() ) then
							local aimvector =	ply:GetCursorAimVector( )
							local aimangle =	aimvector:Angle( )
							local vector = Vector(0,0,0)
							local oldproppower = prop.power
							local name = ply:GetName()
							if servertable["ply"][plyuni]["Autorun"] then
								prop.power = prop.power + 3 
							else
								if (ply:KeyDown(IN_FORWARD)) then prop.power = prop.power + 3 
									elseif ( prop.power > 49 ) then prop.power = prop.power -50 
									elseif ( prop.power > 0 ) then prop.power = prop.power -1 
								end
							end
							if (ply:KeyDown(IN_BACK)) then prop.power =  - 50 
								else if ( prop.power < -9 ) then prop.power = prop.power + 10 end
							end
							if (ply:KeyDown(IN_MOVELEFT)) then local forward = prop:GetRight( ) * -50 vector = vector + forward	end
							if (ply:KeyDown(IN_MOVERIGHT)) then	local forward = prop:GetRight( ) * 50 vector = vector + forward	end
							if (ply:KeyDown(IN_JUMP)) then local forward = prop:GetUp( ) * 50 vector = vector + forward	end
							if (ply:KeyDown(IN_DUCK)) then local forward = prop:GetUp( ) * -50 vector = vector + forward end
							if (prop.power > prop.maxpower) then prop.power = prop.maxpower	end
							if (prop.power < -prop.maxpower) then prop.power = -prop.maxpower end
							local forward = prop:GetForward( ) * prop.power
							vector = vector + forward
							local proppos = prop:GetPos()
							if (CheckPoint(proppos.x,proppos.y,proppos.z,"Station") or CheckPoint(proppos.x,proppos.y,proppos.z,"StationBig")) then
								if (oldproppower != prop.power) then
									local rp = RecipientFilter()
									rp:AddPlayer(ply)
									umsg.Start("SpeedChange", rp)
									umsg.Long(prop.power*0.5)
									umsg.End()
									oldproppower = prop.power*0.5
								end
								prop.Pos = prop:GetPos() + vector*0.5
							else
								if (oldproppower != prop.power) then
									local rp = RecipientFilter()
									rp:AddPlayer(ply)
									umsg.Start("SpeedChange", rp)
									umsg.Long(prop.power)
									umsg.End()
								end
								prop.Pos = prop:GetPos() + vector
							end
							if servertable["ply"][plyuni]["Flymode"] then
								prop.Angles = aimangle
							end
						else
							prop.Pos = prop:GetPos()
							prop.Angles = prop:GetAngles()
						end
					end
				else
					table.remove(servertable["ship"]["controller"],k)
				end
			end
			local gap = 100
			local gtstart = ply:GetShootPos() + ply:GetUp( )*50 + aimvector * gap
			local gaptrace = util.QuickTrace(gtstart, aimvector * 15000, {ply.Vehicle})
			local targetpos = gaptrace.HitPos
			if not ply.groupstofire then ply.groupstofire = 1 end
			if (ply:KeyDown(IN_ATTACK2)) then
				if not ply.nextchange then 
					ply.nextchange = 0 
				end
				if (ply.nextchange < CurTime()) then
					if (ply.groupstofire > 4) then
						ply.groupstofire = 1
					else
						ply.groupstofire = ply.groupstofire + 1
					end
				ply:SendLua("GAMEMODE:AddNotify(\"" ..  "Changed Weapon Group to: " .. ply.groupstofire .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
				ply.nextchange = CurTime() + 0.75
				end
			end
			local fireit = ply:KeyDown(IN_ATTACK)
			if (CheckPoint(targetpos.x,targetpos.y,targetpos.z,"Station") or CheckPoint(targetpos.x,targetpos.y,targetpos.z,"StationBig")) then
				if fireit then	
					ply:PrintMessage(HUD_PRINTCENTER, "You are not allowed to shoot at stations" )
				end
			else
				ply.notinstation = true
				local playerid = ply:UniqueID()
				for k,weapon in pairs(servertable["ship"]["weapons"]) do 
					if (weapon:IsValid())then
						if IsOwner(weapon,ply) then
							weapon.fire = false
							if fireit then
								local turpos = weapon:GetPos()
								if ply.notinstation then
									if servertable["ply"][playerid]["InStation"] or servertable["ply"][playerid]["InBigStation"] then
										ply.notinstation = false
										ply:PrintMessage(HUD_PRINTCENTER, "Shooting in Stations is not allowed")
									else
										if table.HasValue(servertable["ply"][plyuni]["weapons"][ply.groupstofire],weapon:GetClass()) then
											weapon.correct = (targetpos - turpos):Normalize()
											weapon.angles = (targetpos - turpos):Angle()
											weapon.fire = true
											weapon.target = targetpos
											weapon.targetent = gaptrace.Entity
										end
									end
								end
							end
						end
					else
						table.remove(servertable["ship"]["weapons"],k)
					end
				end
			end
		end
	end
end

function Turretmode(ply,bla,args)
	local plyuni = ply:UniqueID()
	local value = util.tobool(args[1])
	servertable["ply"][plyuni]["Flymode"] = not value
	local rp = RecipientFilter()
	rp:AddPlayer(ply)
	if value then
		umsg.Start("AddAlarm", rp)
		umsg.String("Turretmode")
		umsg.End()
	else
		umsg.Start("RemoveAlarm", rp)
		umsg.String("Turretmode")
		umsg.End()
	end
end
concommand.Add("Turretmode",Turretmode)

function Flymode(ply,bla,args)
	local plyuni = ply:UniqueID()
	local value = util.tobool(args[1])
	servertable["ply"][plyuni]["Autorun"] = value
	local rp = RecipientFilter()
	rp:AddPlayer(ply)
	if value then
		umsg.Start("AddAlarm", rp)
		umsg.String("GunMode")
		umsg.End()
	else
		umsg.Start("RemoveAlarm", rp)
		umsg.String("GunMode")
		umsg.End()
	end
end
concommand.Add("Flymode",Flymode)